﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using filmquiz.Src.Domain;
using filmquiz.Src.DatabaseAccess;
using System.Web.SessionState;

namespace filmquiz.Src.Login
{
    public class LoginController
    {
        private User loggedInUser;
        private DbAcces dbAccess;
        private UserHelper userHelper;

        public LoginController()
        {
            dbAccess = DbAcces.getInstance();
            loggedInUser = null;
            userHelper = UserHelper.getInstance();
        }

        public Boolean DoLogin(String username, String password)
        {
            if (userHelper.checkLogin(username, password))
            {
                loggedInUser = userHelper.getUser(username);
                return true;
            }
            return false;
        }

        public Boolean DoLoginFromCookie(string username, int userID, HttpSessionState session)
        {
            loggedInUser = userHelper.getUserByIdAndName(username, userID);
            if (loggedInUser != null)
            {
                session["loggedIn"] = true;
                session["user"] = loggedInUser;
                return true;
            }
            else
            {
                return false;
            }
        }

        public Boolean DoesUserExist(string username)
        {
            User user = userHelper.getUser(username);
            return user != null;
        }

        public User getLoggedInUser()
        {
            return loggedInUser;
        }
    }
}